<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { api } from '@/utils/request'
import { useI18n } from 'vue-i18n'

const { t, locale } = useI18n()

// 表单数据
const form = reactive({
  name: '',
  email: '',
  phone: '',
  whatsapp: '',
  subject: '',
  message: ''
})

// 表单验证规则
const rules = {
  name: [
    { required: true, message: t('contact.form.nameRequired'), trigger: 'blur' }
  ],
  email: [
    { required: true, message: t('contact.form.emailRequired'), trigger: 'blur' },
    { type: 'email', message: t('contact.form.emailFormat'), trigger: 'blur' }
  ],
  message: [
    { required: true, message: t('contact.form.messageRequired'), trigger: 'blur' }
  ]
}

const loading = ref(false)
const formRef = ref()

// 提交表单
const submitForm = async () => {
  if (!formRef.value) return

  try {
    await formRef.value.validate()
    loading.value = true

    const response = await api.post('/contact/submit', {
      ...form,
      language: locale.value
    })

    if (response.success) {
      ElMessage.success(t('contact.form.submitSuccess'))
      resetForm()
    }
  } catch (error: any) {
    if (error.response?.data?.message) {
      ElMessage.error(error.response.data.message)
    } else if (typeof error === 'string') {
      ElMessage.error(error)
    } else {
      ElMessage.error(t('contact.form.submitError'))
    }
  } finally {
    loading.value = false
  }
}

// 重置表单
const resetForm = () => {
  if (formRef.value) {
    formRef.value.resetFields()
  }
  Object.keys(form).forEach(key => {
    form[key as keyof typeof form] = ''
  })
}
</script>

<template>
  <div class="contacts-view">
    <section class="contacts">
      <div class="container">
        <div class="contact-header">
          <h2>{{ $t('contact.title') }}</h2>
          <p class="subtitle">{{ $t('contact.subtitle') }}</p>
        </div>

        <div class="contact-content">
          <div class="contact-form-wrapper">
            <el-form
              ref="formRef"
              :model="form"
              :rules="rules"
              label-width="120px"
              class="contact-form"
            >
              <el-form-item :label="$t('contact.form.name')" prop="name">
                <el-input
                  v-model="form.name"
                  :placeholder="$t('contact.form.namePlaceholder')"
                  clearable
                />
              </el-form-item>

              <el-form-item :label="$t('contact.form.email')" prop="email">
                <el-input
                  v-model="form.email"
                  type="email"
                  :placeholder="$t('contact.form.emailPlaceholder')"
                  clearable
                />
              </el-form-item>

              <el-form-item :label="$t('contact.form.phone')">
                <el-input
                  v-model="form.phone"
                  :placeholder="$t('contact.form.phonePlaceholder')"
                  clearable
                />
              </el-form-item>

              <el-form-item :label="$t('contact.form.whatsapp')">
                <el-input
                  v-model="form.whatsapp"
                  :placeholder="$t('contact.form.whatsappPlaceholder')"
                  clearable
                />
              </el-form-item>

              <el-form-item :label="$t('contact.form.subject')">
                <el-input
                  v-model="form.subject"
                  :placeholder="$t('contact.form.subjectPlaceholder')"
                  clearable
                />
              </el-form-item>

              <el-form-item :label="$t('contact.form.enquiry')" prop="message">
                <el-input
                  v-model="form.message"
                  type="textarea"
                  :rows="6"
                  :placeholder="$t('contact.form.enquiryPlaceholder')"
                  show-word-limit
                  maxlength="1000"
                />
              </el-form-item>

              <el-form-item>
                <el-button 
                  type="primary" 
                  @click="submitForm"
                  :loading="loading"
                  size="large"
                  class="submit-btn"
                >
                  {{ $t('contact.form.submit') }}
                </el-button>
                <el-button @click="resetForm" size="large">
                  {{ $t('contact.form.reset') }}
                </el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
    </section>
  </div>
</template>

<style scoped>
.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

/* Contacts Section */
.contacts {
  padding: 80px 0;
  background: #f8f9fa;
  min-height: 80vh;
}

.contact-header {
  text-align: center;
  margin-bottom: 60px;
}

.contact-header h2 {
  color: #2c3e50;
  font-size: 36px;
  margin-bottom: 20px;
  font-weight: 600;
}

.contact-header .subtitle {
  color: #666;
  font-size: 18px;
  line-height: 1.8;
  margin: 0;
}

.contact-content {
  display: flex;
  justify-content: center;
}

.contact-form-wrapper {
  background: white;
  padding: 50px;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  width: 100%;
  max-width: 700px;
}

.contact-form {
  width: 100%;
}

:deep(.el-form-item__label) {
  font-weight: 600;
  color: #2c3e50;
  font-size: 15px;
}

:deep(.el-input__wrapper) {
  border-radius: 8px;
  padding: 12px 16px;
}

:deep(.el-textarea__inner) {
  border-radius: 8px;
  padding: 12px 16px;
  font-family: inherit;
}

:deep(.el-button) {
  border-radius: 8px;
  padding: 12px 30px;
  font-size: 16px;
  font-weight: 600;
}

.submit-btn {
  margin-right: 20px;
}

:deep(.el-button--primary) {
  background: #27ae60;
  border-color: #27ae60;
}

:deep(.el-button--primary:hover) {
  background: #229954;
  border-color: #229954;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .contacts {
    padding: 40px 0;
  }
  
  .contact-header h2 {
    font-size: 28px;
  }
  
  .contact-form-wrapper {
    padding: 30px 20px;
    margin: 0 10px;
  }
  
  .contact-form :deep(.el-form-item__label) {
    width: 100px !important;
  }
  
  .submit-btn {
    margin-right: 10px;
  }
  
  :deep(.el-button) {
    padding: 10px 20px;
    font-size: 14px;
  }
}

@media (max-width: 480px) {
  .contact-form-wrapper {
    padding: 20px 15px;
  }
  
  .contact-form :deep(.el-form-item) {
    margin-bottom: 20px;
  }
  
  .contact-form :deep(.el-form-item__label) {
    width: 80px !important;
    font-size: 14px;
  }
  
  :deep(.el-button) {
    width: 100%;
    margin-right: 0 !important;
    margin-bottom: 10px;
  }
}
</style>